package com.pro_test.proservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pro_test.commonutils.R;
import com.pro_test.proservice.entity.ProComplaint;
import com.pro_test.proservice.entity.ProRepair;
import com.pro_test.proservice.entity.vo.ComplaintQuery;
import com.pro_test.proservice.entity.vo.RepairQuery;
import com.pro_test.proservice.service.ProComplaintService;
import com.pro_test.proservice.service.ProRepairService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Captain
 * @since 2022-02-26
 */
@RestController
@RequestMapping("/proservice/pro-repair")
@CrossOrigin
public class ProRepairController {

    @Autowired
    private ProRepairService proRepairService;

    @ApiOperation(value = "查询所有修理")
    @GetMapping("findAll")
    public R findAllRepair()
    {
        List<ProRepair> list = proRepairService.list(null);
        return R.ok().data("items",list);
    }

    @ApiOperation(value = "逻辑删除修理")
    @DeleteMapping("{id}")
    public R removeRepair(@ApiParam(name = "id", value = "修理ID", required = true)
                             @PathVariable String id)
    {
        boolean flag = proRepairService.removeById(id);
        if (flag)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }

    //条件查询分页
    @ApiOperation(value = "条件分页查询")
    @PostMapping("pageRepairCondition/{current}/{limit}")
    public R pageRepairCondition(@PathVariable long current,
                                    @PathVariable long limit,
                                    @RequestBody(required = false) RepairQuery repairQuery)
    {
        //创建page对象
        Page<ProRepair> pageRepair = new Page<>(current,limit);

        //构建条件
        QueryWrapper<ProRepair> wrapper = new QueryWrapper<>();
        String status = repairQuery.getStatus();
        String handler = repairQuery.getHandler();
        String begin = repairQuery.getBegin();
        String end = repairQuery.getEnd();

        if (!StringUtils.isEmpty(handler))
        {
            wrapper.like("handler",handler);
        }
        if (!StringUtils.isEmpty(status))
        {
            wrapper.eq("status",status);
        }
        if (!StringUtils.isEmpty(begin))
        {
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end))
        {
            wrapper.le("gmt_create",end);
        }
        //排序
        wrapper.orderByDesc("gmt_create");

        //调用方法实现条件查询分页
        proRepairService.page(pageRepair,wrapper);
        long total = pageRepair.getTotal();
        List<ProRepair> records = pageRepair.getRecords();

        return R.ok().data("total",total).data("items",records);
    }

    @ApiOperation(value = "添加修理")
    @PostMapping("addRepair")
    public R addRepair(@RequestBody ProRepair proRepair)
    {
        boolean save = proRepairService.save(proRepair);
        if (save)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }

    @ApiOperation(value = "查询修理")
    @GetMapping("getRepair/{id}")
    public R getRepair(@PathVariable String id)
    {
        ProRepair proRepair = proRepairService.getById(id);
        return R.ok().data("repair",proRepair);
    }

    @ApiOperation(value = "修改修理")
    @PostMapping("updateRepair")
    public R updateRepair(@RequestBody ProRepair proRepair)
    {
        boolean flag = proRepairService.updateById(proRepair);
        if (flag)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }
}

